package com.uzai.console.web.wxpub;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.enums.WxpubDeviceProductKeyEnum;
import com.uzai.common.enums.WxworkProductKeyEnum;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.BaseDto;
import com.uzai.console.dto.wechatprivate.configplan.ConfigPlanCopyDefaultDto;
import com.uzai.console.dto.wxpub.DeviceWxpubInfoBuyAuthDto;
import com.uzai.console.dto.wxpub.PriceDto;
import com.uzai.console.dto.wxpub.WxpubAmountDto;
import com.uzai.console.dto.wxpubmanage.WxpubComponentDto;
import com.uzai.console.dto.wxpubmanage.WxpubDeviceBindDto;
import com.uzai.console.dto.wxpubmanage.WxpubDeviceListQuery;
import com.uzai.console.dto.wxpubmanage.WxpubAddAuthFreeDto;
import com.uzai.console.dto.wxpubmanage.WxpubInfoDeleteDto;
import com.uzai.console.dto.wxpubmanage.WxpubInfoListQuery;
import com.uzai.console.dto.wxpubmanage.WxpubInfoUpdateDto;
import com.uzai.console.dto.wxpubmanage.WxpubInfoUpdateSingleDto;
import com.uzai.console.entity.DeviceProduct;
import com.uzai.console.entity.wxpubmanage.WxpubComponentEntity;
import com.uzai.console.entity.wxpubmanage.WxpubInfoEntity;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.enums.WxpubDeviceProductEnum;
import com.uzai.console.enums.WxworkDeviceProductEnum;
import com.uzai.console.mapper.DeviceProductMapper;
import com.uzai.console.mapper.ProductPriceMapper;
import com.uzai.console.service.wxpubmanage.WxpubComponentService;
import com.uzai.console.service.wxpubmanage.WxpubInfoService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.deviceproduct.DeviceProductVo;
import com.uzai.console.vo.wechatprivate.configplan.ConfigPlanVo;
import com.uzai.console.vo.wechatprivate.devicewechatinfo.DeviceAmountVo;
import com.uzai.console.vo.wechatprivate.devicewechatinfo.DevicePriceVo;
import com.uzai.console.vo.wechatprivate.devicewechatinfo.DeviceWechatInfoAddVo;
import com.uzai.console.vo.wxpub.AuthUrlVo;
import com.uzai.console.vo.wxpub.DeviceInfoVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 微信公众号管理
 *
 * @author cen
 * @date 2022/2/27 15:20
 */
@Api(value = "微信公众号管理", tags = "微信公众号管理", hidden = true)
@ApiSort(999)
@RestController
public class WxpubManageController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(WxpubManageController.class);

    @Autowired
    private WxpubInfoService wxpubInfoService;
    @Autowired
    private WxpubComponentService wxpubComponentService;
    @Autowired
    private ProductPriceMapper productPriceMapper;
    @Autowired
    private DeviceProductMapper deviceProductMapper;


    @ApiOperation(value = "查询微信三方平台配置信息", notes = "查询微信三方平台配置信息", response = WxpubComponentEntity.class)
    @PostMapping("/wxpub/thirdPlatform/queryInfo")
    public Object queryThirdPlatformInfo(@RequestBody BaseDto baseDto){
        return wxpubComponentService.queryObject(baseDto.getMerId());
    }

    @ApiOperation(value = "修改微信三方平台配置信息", notes = "修改微信三方平台配置信息" ,response = Boolean.class)
    @PostMapping("/wxpub/thirdPlatform/updateInfo")
    public Object updateThirdPlatformInfo(@RequestBody WxpubComponentDto wxpubComponentDto){
        wxpubComponentService.updateObject(wxpubComponentDto);
        return true;
    }

    @ApiOperation(value = "查询授权公众号列表", notes = "修改微信三方平台配置信息",response = WxpubInfoEntity.class)
    @PostMapping("/wxpub/auth/queryList")
    public Object queryAuthList(@RequestBody WxpubInfoListQuery wxpubInfoListQuery){
        return wxpubInfoService.queryList(wxpubInfoListQuery);
    }

    @ApiOperation(value = " 公众号信息单条数据修改", notes = "公众号信息单条数据修改",response = Boolean.class)
    @PostMapping("/wxpub/auth/updateSingle")
    public Object updateWxpubObject(@RequestBody WxpubInfoUpdateSingleDto wxpubInfoUpdateSingleDto){
        wxpubInfoService.updateWxpubObject(wxpubInfoUpdateSingleDto);
        return true;
    }

    @ApiOperation(value = "批量修改微信公众号配置信息", notes = "批量修改微信公众号配置信息",response = Boolean.class)
    @PostMapping("/wxpub/auth/updateList")
    public Object updateWxpubInfo(@RequestBody WxpubInfoUpdateDto wxpubInfoUpdateDto){
        wxpubInfoService.updateList(wxpubInfoUpdateDto);
        return true;
    }

    @ApiOperation(value = "查询详情", notes = "查询详情", response = WxpubInfoEntity.class)
    @PostMapping("/wxpub/auth/queryInfo")
    public Object updateWxpubInfo(@RequestBody WxpubAddAuthFreeDto wxpubIdDto) {
        return wxpubInfoService.queryObject(wxpubIdDto.getId());
    }

    @ApiOperation(value = "获取授权链接 ", notes = "获取授权链接", response = AuthUrlVo.class)
    @PostMapping("/wxpub/auth/queryAuthUrl")
    public Object queryAuthUrl(@RequestBody BaseDto baseDto) {
        return wxpubInfoService.queryAuthUrl(baseDto);
    }

    @ApiOperation(value = "批量删除  ", notes = "执行批量解绑操作 ", response = AuthUrlVo.class)
    @PostMapping("/wxpub/auth/deleteList")
    public Object deleteList(@RequestBody WxpubInfoDeleteDto wxpubInfoDeleteDto) {
        wxpubInfoService.deleteList(wxpubInfoDeleteDto);
        return null;
    }

    @ApiOperation(value = "查询机器人列表分页  ", notes = "查询机器人列表分页 ", response = DeviceInfoVo.class)
    @PostMapping("/wxpub/device/queryUnBindList")
    public Object queryUnBindDeviceList(@RequestBody WxpubDeviceListQuery wxpubDeviceListQuery) {
        return wxpubInfoService.queryDeviceInfo(wxpubDeviceListQuery);
    }

    @ApiOperation(value = "绑定机器人  ", notes = "绑定机器人 ", response = DeviceInfoVo.class)
    @PostMapping("/wxpub/device/bindDeviceList")
    public Object bindDeviceList(@RequestBody WxpubDeviceBindDto wxpubDeviceBindDto) {
         wxpubInfoService.bindDeviceList(wxpubDeviceBindDto);
        return true;
    }

    @ApiOperation(value = "根据条件进行全绑定", notes = "根据条件进行全绑定 ", response = DeviceInfoVo.class)
    @PostMapping("/wxpub/device/bindAllDeviceList")
    public Object bindAllDeviceList(@RequestBody WxpubDeviceListQuery wxpubDeviceListQuery) {
         wxpubInfoService.bindAllDeviceList(wxpubDeviceListQuery);
        return true;
    }

    @ApiOperation(value = "机器人价格查询", notes = "机器人价格查询", response = DevicePriceVo.class)
    @ApiOperationSupport(order = 17)
    @PostMapping("/wxpub/getPrice")
    public Object getPrice(@RequestBody PriceDto priceDto){
        return wxpubInfoService.getPrice(priceDto);
    }

    @SystemLogType(model = SystemModelEnum.DeviceWxpubInfo, type = OperationTypeEnum.BUY)
    @ApiOperation(value = "公众号购买授权", notes = "公众号购买授权", response = DeviceWechatInfoAddVo.class)
    @ApiOperationSupport(order = 18)
    @PostMapping("/wxpub/buyDeviceAuth")
    public Object buyDeviceAuth(@RequestBody DeviceWxpubInfoBuyAuthDto deviceWxpubInfoBuyAuthDto){
        return wxpubInfoService.buyDeviceAuth(deviceWxpubInfoBuyAuthDto);
    }

    @ApiOperation(value = "查询购买授权总金额", notes = "查询购买授权总金额", response = DeviceAmountVo.class)
    @ApiOperationSupport(order = 19)
    @PostMapping("/wxpub/getAmount")
    public Object getAmount(@RequestBody WxpubAmountDto wxpubAmountDto){
        return wxpubInfoService.getAmount(wxpubAmountDto);
    }

    @ApiOperation(value = "添加微信公众号免费试用期授权", notes = "添加微信公众号免费试用期授权", response = DeviceAmountVo.class)
    @ApiOperationSupport(order = 20)
    @PostMapping("/wxpub/addWxpubAuthFree")
    public Object addWxpubAuthFree(@RequestBody WxpubAddAuthFreeDto wxpubAddAuthFreeDto){
        wxpubInfoService.addWxpubAuthFree(wxpubAddAuthFreeDto);
        return true;
    }

    @ApiOperation(value = "查询公众号购买产品列表", notes = "查询公众号购买产品列表", response = DeviceProductVo.class)
    @ApiOperationSupport(order = 21)
    @PostMapping("/wxpub/deviceProductList")
    public Object deviceProductList(){
        List<DeviceProductVo> deviceProductVoList = new ArrayList<>();
        WxpubDeviceProductEnum[] wxpubDeviceProductEnums = WxpubDeviceProductEnum.values();
        if(wxpubDeviceProductEnums != null && wxpubDeviceProductEnums.length > 0){
            for (WxpubDeviceProductEnum wxpubDeviceProductEnum : wxpubDeviceProductEnums){
                DeviceProductVo deviceProductVo = new DeviceProductVo();
                DeviceProduct deviceProduct = deviceProductMapper.selectByCode(wxpubDeviceProductEnum.getValue());
                if(deviceProduct != null){
                    BeanUtils.copyProperties(deviceProduct, deviceProductVo);
                }

                deviceProductVo.setName(wxpubDeviceProductEnum.getDesc());

                if(StringUtils.isNotBlank(wxpubDeviceProductEnum.getCodes())){
                    deviceProductVo.setCodeList(JSONObject.parseArray(wxpubDeviceProductEnum.getCodes(), String.class));
                }
                deviceProductVoList.add(deviceProductVo);
            }
        }
        return deviceProductVoList;
    }

    @ApiOperation(value = "查询公众号关联产品列表", notes = "查询公众号关联产品列表")
    @ApiOperationSupport(order = 21)
    @PostMapping("/wxpub/findDeviceProductList")
    public Object findDeviceProductList(){
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        WxpubDeviceProductKeyEnum[] wxpubDeviceProductKeyEnums = WxpubDeviceProductKeyEnum.values();
        if(wxpubDeviceProductKeyEnums != null){
            for (WxpubDeviceProductKeyEnum wxpubDeviceProductKeyEnum : wxpubDeviceProductKeyEnums){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(wxpubDeviceProductKeyEnum.getValue());
                selectKeyVo.setValue(wxpubDeviceProductKeyEnum.getDesc());
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }

}
